$.ajax({
  type: 'get',
  url: '/posts/random',
  success: function (res) {
    console.log(res);
    var randomTpl = `
    {{each data}}
        <li>
            <a href="/detail.html?id={{$value._id}}">
              <p class="title">{{$value.title}}</p>
              <p class="reading">阅读({{$value.views}})</p>
              <div class="pic">
                <img src="{{$value.thumbnail}}" alt="">
              </div>
            </a>
          </li>
          {{/each}}
          `;
    var html = template.render(randomTpl, { data: res })
    $('#randomBox').html(html);
  }
});
$.ajax({
  type: 'get',
  url: '/comments/lasted',
  success: function (response) {
    var commentTpl = ` 
        {{each data}} 
        <li>
        <a href="javascript:;">
        <div class="avatar">
        <img src="{{$value.author.avatar}}" alt="">
        </div>
        <div class="txt">
        <p> <span>{{$value.author.nickName}}</span>
        {{$value.createAt.split('T')[0]}}说: </p>
        <p>{{$value.content}}</p>
        </div>
        </a>
        </li>
        {{/each}} `;
    var html = template.render(commentTpl, { data: response });
    $('#commentBox').html(html);
  }
})
$.ajax({
  type: 'get',
  url: '/categories',
  success: function (res) {
    console.log(res);
    var tpl = ` 
				{{each data}}
					<li><a href="list.html?categoryId={{$value._id}}"><i class="fa {{$value.className}}"></i>{{$value.title}}</a></li>
				{{/each}}
		`
    var html = template.render(tpl, { data: res })
    $('.header .nav').html(html)
  }
})
$('.search form').on('submit', function () {
  var key = $(this).find('.keys').val()//搜索的关键字
  location.href = 'search.html?key=' + key;
  return false;
})